package com.tuanzi.loan.business.service.statistics.impl;

import java.text.DecimalFormat;
import java.util.List;

import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.repository.statistics.ActivityAverageInterceptStatRepository;
import com.tuanzi.loan.business.service.statistics.ActivityAverageJoinInterceptStatService;
import com.tuanzi.loan.business.service.statistics.ActivityParticipateStatService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.statistics.ActivityAverageJoinInterceptStatRequest;
import com.tuanzi.loan.vo.request.statistics.ActivityAverageJoinStatRequest;
import com.tuanzi.loan.vo.response.statistics.ActivityAverageJoinInterceptStatResponse;
import com.tuanzi.server.loan_base.model.bean.common.CommAdvertise;
import com.tuanzi.server.loan_base.model.bean.common.CommPlatform;
import com.tuanzi.server.loan_base.model.bean.common.CommUserType;
import com.tuanzi.server.loan_base.model.db.CommProductChannel;
import com.tuanzi.server.loan_base.model.db.CommProductPrdid;
import com.tuanzi.server.loan_base.model.db.LoanProductInfo;

@Debug
@Service
@Transactional
@Retryable(value = { RetryException.class })
public class ActivityAverageJoinInterceptStatServiceImpl implements ActivityAverageJoinInterceptStatService {
	
	@Autowired
	private ActivityAverageInterceptStatRepository activityAverageInterceptStatRepository;
	
	@Autowired
	private ActivityParticipateStatService activityParticipateStatService;
	
	private TreeNode prodNodeTree;
	
	public String getExcel(ActivityAverageJoinInterceptStatRequest searchRequest) {
		searchRequest.setProductIdList(getProdList(searchRequest));
		// 参与统计
		List<ActivityAverageJoinInterceptStatResponse> list = activityAverageInterceptStatRepository
					.getAllList(searchRequest);
    	StringBuffer sf = new StringBuffer();
		sf.append("时间,产品,渠道,平台,用户类型,广告ID,活动,日活UV,登陆UV,详情UV,参与UV,人均详情,人均参与,参与/详情\r\n");
		if(list != null) {
			for(ActivityAverageJoinInterceptStatResponse response : list) {
				sf.append(response.getDate()+",");
				sf.append(response.getProductName()+",");
				sf.append(response.getChannelName()+",");
				sf.append(response.getPlatform()+",");
				sf.append(response.getUserType()+",");
				sf.append(response.getAdName()+",");
				sf.append(response.getActivityName()+",");
				sf.append(response.getActivityUv()+",");
				sf.append(response.getLoginUv()+",");
				sf.append(response.getShowUv()+",");
				sf.append(response.getClickUv()+",");
				sf.append(response.getPerShowAct()+",");
				sf.append(response.getPerClickAct()+",");
				sf.append(response.getPerClickShow());
				sf.append("\r\n");
			}
		}
    	
    	return sf.toString();
    }
	
	public Page<ActivityAverageJoinInterceptStatResponse> list(PageSearcher<ActivityAverageJoinInterceptStatRequest> pageSearcher) {
		pageSearcher.getSearchCondition().setProductIdList(getProdList(pageSearcher.getSearchCondition()));
		Page<ActivityAverageJoinInterceptStatResponse> activityJoinListPage = activityAverageInterceptStatRepository
				.getlistPage(pageSearcher.getSearchCondition(), pageSearcher.getPageable());
		return activityJoinListPage;
	}
	
	/**
	 * 将选中产品的树形结构转换为list
	 * @param searchRequest
	 * @return
	 */
	private List<Integer> getProdList(ActivityAverageJoinInterceptStatRequest searchRequest) {
		TreeNode[] nodes = searchRequest.getProdNodes();
		if(nodes == null || nodes.length == 0) {
			return null;
		}
		
		List<Integer> selectedProdList = Lists.newArrayList();
		for(TreeNode node : nodes) {
			CommProductPrdid prod = (CommProductPrdid) node.getData();
			if(prod.getLevel() == 2) {
				selectedProdList.add(prod.getPrdid());
			}
		}
		return selectedProdList;
	}
	
	@Override
    public void cleanCache() {
    	activityParticipateStatService.cleanCache();
    }

    @Override
    public List<CommProductPrdid> getProduct() {
        return activityAverageInterceptStatRepository.getProduct();
    }

    @Override
    public List<CommProductChannel> getChannel() {
        return activityAverageInterceptStatRepository.getChannel();
    }

    @Override
    public List<CommPlatform> getPlatform() {
        return activityAverageInterceptStatRepository.getPlatform();
    }

    @Override
    public List<CommUserType> getUserType() {
        return activityAverageInterceptStatRepository.getUserType();
    }

    @Override
    public List<CommAdvertise> getAD() {
        return activityAverageInterceptStatRepository.getSpace();
    }

    @Override
    public List<LoanProductInfo> getActivity() {
        return activityAverageInterceptStatRepository.getActivity();
    }

	@Override
	public TreeNode getProductTree() {
		return activityParticipateStatService.getProductTree();
	}
	
}

























